package com.zyh.stream;

import com.zyh.domain.Student;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zyh
 * @Date 2024/8/16 15:45
 * @Description 收集
 */

public class StreamDemo7 {
    //统计
    @Test
    public void test1() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("zyh", "男", 18, 170.5));
        list.add(new Student("wy", "男", 20, 140.5));
        list.add(new Student("cxk", "女", 45, 120.5));
        list.add(new Student("dz", "女", 2, 180.5));
        list.add(new Student("zhangSan", "男", 13, 146.5));
        list.add(new Student("li4", "女", 56, 189.5));
        list.add(new Student("wang5", "男", 24, 110.5));
        //统计集合中的元素个数
        Long count1 = list.stream().collect(Collectors.counting());
        System.out.println("count1 = " + count1);
        //统计性别为男的学生个数
        Long count2 = list.stream().filter(i -> "男".equals(i.getSex())).collect(Collectors.counting());
        System.out.println("count2 = " + count2);
        //求所有女生的年龄和
        Integer ageSum = list.stream().filter(i -> "女".equals(i.getSex())).collect(Collectors.summingInt(i -> i.getAge()));
        System.out.println("ageSum = " + ageSum);
        //求身高的最大值
        Double maxHeight = list.stream().collect(Collectors.maxBy((i1, i2) -> (int) (i1.getHeight() - i2.getHeight()))).get().getHeight();
        System.out.println("maxHeight = " + maxHeight);
        //求男生身高平均值
        Double avg = list.stream().collect(Collectors.averagingDouble(i -> i.getHeight()));
        System.out.println("avg = " + avg);
        //一次性拿到全部信息(age)
        DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(Student::getAge));
        System.out.println("collect = " + collect);
    }
}
